package com.seio.edc.controller.system;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.nova.edc.model.system.ElecPrice;
import com.nova.edc.model.system.Login;
import com.nova.edc.service.system.ElecPriceTypeService;
import com.nova.frame.dao.pager.Page;
import com.nova.frame.dao.pager.PageList;
import com.nova.frame.utils.DateUtils;
import com.seio.edc.controller.core.BaseContoller;

@Controller
@RequestMapping(value = "/systemElecPrice")
public class ElecPriceContronller extends BaseContoller {

	@Autowired
	private com.nova.edc.service.system.ElecPriceService novaElecPriceService;

	@Autowired
	private ElecPriceTypeService novaElecPriceTypeService;

	private final int PAGE_SIZE = 10;

	@InitBinder("elecPriceSearchEnity")
	public void initBinder2(WebDataBinder binder) {
		binder.setFieldDefaultPrefix("elecPriceSearchEnity.");
	}

	@InitBinder("elecPriceEntity")
	public void initBinder1(WebDataBinder binder) {
		binder.setFieldDefaultPrefix("elecPriceEntity.");
	}

	/**
	 * 用户表页面初始化
	 * 
	 * @param request
	 * @param modelMap
	 * @return
	 */
	@RequestMapping(value = "elecPriceList.shtml")
	public ModelAndView elecPriceList(HttpServletRequest request,
			HttpSession session, Integer pageIndex, ModelMap modelMap,
			@ModelAttribute("elecPrice") ElecPrice elecPrice) {

		// 当前页数
		Page page = new Page();
		page.setCurrentPage(pageIndex == null || pageIndex == 0 ? 1 : pageIndex);
		page.setPageSize(PAGE_SIZE);
		modelMap.put("pageIndex", page.getCurrentPage());
		Login sessionLogin = (Login) session.getAttribute("sessionLogin");
		elecPrice.setLoginid(sessionLogin.getId());
		PageList<ElecPrice> pageList = novaElecPriceService
				.findElecPricePageList(elecPrice, page, "AddDate", "desc");
		modelMap.put("dataCount", page.getRowCount());
		modelMap.put("pageCount", page.getTotalPage());
		modelMap.put("list", pageList.getDatas());

		return new ModelAndView("system/elecPriceList", modelMap);
	}

	/**
	 * 添加初始化
	 * 
	 * @param request
	 * @param modelMap
	 * @return
	 */
	@RequestMapping(value = "addElecPriceInit.shtml")
	public ModelAndView addelecPriceInit(HttpServletRequest request,
			ModelMap modelMap, Integer id) {

		modelMap.put("elecPriceTypes", novaElecPriceTypeService.findAll());
		if (id != null && id != 0) {
			modelMap.put("elecPrice", novaElecPriceService.getById(id));
		}

		return new ModelAndView("system/elecPriceEdit", modelMap);
	}

	/**
	 * 添加elecPrice
	 */
	@RequestMapping(value = "addElecPrice.shtml")
	public ModelAndView addelecPrice(ElecPrice elecPrice, BindingResult result,
			HttpServletRequest request, HttpSession session, ModelMap modelMap) {
		Login login = (Login) session.getAttribute("sessionLogin");
		if (elecPrice.getId() != null && elecPrice.getId() != 0) {
			elecPrice.setModifyby(login.getUsername());
			elecPrice.setModifydate(DateUtils.getCurrentDate());
			elecPrice.setModifystamp(DateUtils.getCurrentDate());
			novaElecPriceService.update(elecPrice);
		} else {
			elecPrice.setLoginid(login.getId());
			elecPrice.setAddby(login.getUsername());
			elecPrice.setAdddate(DateUtils.getCurrentDate());
			novaElecPriceService.insert(elecPrice);
		}

		return elecPriceList(request, session, 1, modelMap, new ElecPrice());

	}

	@RequestMapping(value = "deleteElecPrice.shtml")
	public ModelAndView deleteelecPrice(int id, HttpServletRequest request,
			HttpSession session, ModelMap modelMap, ElecPrice elecPrice) {
		novaElecPriceService.delete(id);
		return elecPriceList(request, session, 1, modelMap, elecPrice);

	}

	@RequestMapping(value = "priceCodeVali.shtml")
	@ResponseBody
	public Map<String, Object> priceCodeVali(HttpServletRequest request,
			Integer ePriceCode, Integer codeId) {

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("existFlag",
				novaElecPriceService.isExistElecPriceCode(ePriceCode, codeId));
		return map;
	}

}
